import { onMounted, ref } from "vue";
import { getSubCategoryAPI } from "@/apis/category";
import { onBeforeRouteUpdate, useRoute } from "vue-router";




export function  useCategory(){
    const categoryData = ref({});

    const route = useRoute();
    const getCategory = async (id = route.params.id) => {
        const res = await getSubCategoryAPI(id);
        categoryData.value = res.result;
    };

    onMounted(() => {
        getCategory()
    });

    //在路由发生变化时候，重新访问分类接口
    onBeforeRouteUpdate((to) => {
        getCategory(to.params.id)
    })

    return {categoryData}
}

